package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sql.DBManagment;
import sql.record.*;
/**
 * Servlet implementation class addCourse
 */
public class addCourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addCourse() {
        super();
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Course newCourse = new Course();
		newCourse.courseName = request.getParameter("courseName");
		newCourse.description = request.getParameter("description");
		newCourse.capacity = new Integer(request.getParameter("capacity"));
		newCourse.creditPoints = new Integer(request.getParameter("creditPoints"));
	
		List<TableRecord> recList = new LinkedList<TableRecord>();
		recList.add(newCourse);
		
		String courseDays[] = request.getParameterValues("dayBox");
		for(String day : courseDays){
			int dayNumber = Integer.parseInt(day);
			CourseSchedule newCourseSchedule = new CourseSchedule();
			newCourseSchedule.courseName = request.getParameter("courseName");
			newCourseSchedule.groupNum =Integer.parseInt(request.getParameter("groupNumber"));
			newCourseSchedule.startHour = new Integer(request.getParameter("day"+ dayNumber + "Begin"));
			newCourseSchedule.endHour = new Integer(request.getParameter("day"+ dayNumber + "End"));
			newCourseSchedule.day = dayNumber; 
			recList.add(newCourseSchedule);
		}
	
		try {
			DBManagment.insertRecord(recList);
			out.print("<firefoxsucks>added</firefoxsucks>");
		} catch (ClassNotFoundException e) {
			//generate 500....
		} catch (SQLException e) {
			out.print("<firefoxsucks>" + e.getMessage() + "</firefoxsucks>");
			
		}
	
		out.close();
	}
}
